//
// ***********************************************
// *    ___________   ___________  __      __    *
// *   |______  __/  /_____  ___/  \ \    / /    *
// *         / /          / /       \ \  / /     *
// *        / /          / /         \ \/ /      *
// *       / /          / /           \  /       *
// *      / /______    / /            / /        *
// *     /________/   /_/            /_/         *
// *                                             *
// ***********************************************
//
//   SubAdViewController.m
//   InterviewDemo
//   
//   Created  by wanmeizty on 26/12/2019
//   Modified by Tiyon  qq 897574226  
//   Copyright © 2019 wanmeizty. All rights reserved.
//   
   

#import "SubAdViewController.h"
#import <PTFakeTouch/PTFakeTouch.h>
//#import "TimerManager.h"
#import <BUAdSDK/BUAdSDK.h>

#define SCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width

static int count = 0;
@interface SubAdViewController ()<BUNativeExpressBannerViewDelegate,BUNativeExpressFullscreenVideoAdDelegate,BUNativeExpressRewardedVideoAdDelegate,BUSplashAdDelegate>
@property (strong,nonatomic) NSTimer * timer;
@property (strong,nonatomic) UIButton * backBtn;

@property (strong,nonatomic) BUNativeExpressFullscreenVideoAd * fullVideoAd;
@property (strong,nonatomic) BUNativeExpressRewardedVideoAd * rewardedAd;
@property (strong,nonatomic) BUSplashAdView *splashView;
@end

@implementation SubAdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
   
    UIImage *btnImg = [[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIButton * btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    [btn setImage:btnImg forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.backBtn = btn;
    self.navigationItem.leftBarButtonItem = backItem;
    
    count = 0;
    [self loadFullVideo];
    [self loadRewardVideo];
    [self loadBannerAd];
    
    
    // Do any additional setup after loading the view.
}

-(void)backClick{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (_timer == nil) {
        
        _timer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(fire) userInfo:nil repeats:YES];
        
    }else{
        [_timer setFireDate:[NSDate distantPast]];
    }
}

-(void)fire{
    NSLog(@"count===>%ld",count);
    if (count % 9 == 0) {
        [self loadBannerAd];
        count ++;
    }else if (count % 9 == 1){
        [self loadGuideAd];
        
    }else if (count % 9 == 2){
        
        if (self.fullVideoAd.adValid) {
            [self.fullVideoAd showAdFromRootViewController:self];
        }else{
            CGPoint point = CGPointMake(self.view.frame.size.width - 30,30);
            NSInteger pointId = [PTFakeTouch fakeTouchId:[PTFakeTouch getAvailablePointId] AtPoint:point withTouchPhase:UITouchPhaseBegan];
            [PTFakeTouch fakeTouchId:pointId AtPoint:point withTouchPhase:UITouchPhaseEnded];
        }
        
    }else if (count % 9 == 3){
        
        [self loadBannerAd];
        count ++ ;
    }else if (count % 9 == 4){
        [self loadGuideAd];
        
    }else if (count % 9 == 5){
        if (self.rewardedAd.adValid) {
            [self.rewardedAd showAdFromRootViewController:self];
        }else{
            CGPoint point = CGPointMake(self.view.frame.size.width - 30,30);
            NSInteger pointId = [PTFakeTouch fakeTouchId:[PTFakeTouch getAvailablePointId] AtPoint:point withTouchPhase:UITouchPhaseBegan];
            [PTFakeTouch fakeTouchId:pointId AtPoint:point withTouchPhase:UITouchPhaseEnded];
        }
        
    }else if (count % 9 == 6){
        [self loadGuideAd];
        
    }else if (count % 9 == 7){
        [self loadBannerAd];
        count ++ ;
    }else if (count % 9 == 8){
        [self loadGuideAd];
        
    }else{
        count ++ ;
    }
    
    if (count == 35) {
        [self.backBtn sendActionsForControlEvents:UIControlEventTouchUpInside];
        if (_timer) {
            [_timer invalidate];
            _timer = nil;
        }
        
        
    }
}

-(void)loadGuideAd{
    [BUAdSDKManager setAppID:@"5040033"];
    [BUAdSDKManager setIsPaidApp:NO];
    //    [BUAdSDKManager setLoglevel:BUAdSDKLogLevelDebug];
    
    
    CGRect frame = [UIScreen mainScreen].bounds;
    BUSplashAdView *splashView = [[BUSplashAdView alloc] initWithSlotID:@"840033585" frame:frame];
    splashView.delegate = self;
    
    [splashView loadAdData];
    [self.view addSubview:splashView];
    self.splashView = splashView;
    splashView.rootViewController = self;
}

- (void)splashAdDidLoad:(BUSplashAdView *)splashAd{
    
}
- (void)splashAd:(BUSplashAdView *)splashAd didFailWithError:(NSError * _Nullable)error{
    
}
- (void)splashAdWillVisible:(BUSplashAdView *)splashAd{
    
}
- (void)splashAdDidClick:(BUSplashAdView *)splashAd{
    
}
- (void)splashAdDidClose:(BUSplashAdView *)splashAd{
    [splashAd removeFromSuperview];
    count ++;
}
- (void)splashAdWillClose:(BUSplashAdView *)splashAd{
    
}
- (void)splashAdDidCloseOtherController:(BUSplashAdView *)splashAd interactionType:(BUInteractionType)interactionType{
    
}

-(void)loadRewardVideo{
    
    //    BURewardedVideoModel *model = [[BURewardedVideoModel alloc] init];
    //    model.userId = @"123456";
    //    BURewardedVideoAd * videoView = [[BURewardedVideoAd alloc] initWithSlotID:@"940033495" rewardedVideoModel:model];
    //    videoView.delegate = self;
    //    [videoView loadAdData];
    
    BURewardedVideoModel *model = [[BURewardedVideoModel alloc] init];
    model.userId = @"123";
    BUNativeExpressRewardedVideoAd *rewardedAd = [[BUNativeExpressRewardedVideoAd alloc] initWithSlotID:@"940033495" rewardedVideoModel:model];
    rewardedAd.delegate = self;
    self.rewardedAd = rewardedAd;
    [rewardedAd loadAdData];
    
}

- (void)nativeExpressRewardedVideoAdDidLoad:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd{
    
}
- (void)nativeExpressRewardedVideoAd:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd didFailWithError:(NSError *_Nullable)error{
    
}
- (void)nativeExpressRewardedVideoAdDidDownLoadVideo:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd{
    
}
- (void)nativeExpressRewardedVideoAdViewRenderSuccess:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd{
    
}
- (void)nativeExpressRewardedVideoAdViewRenderFail:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd error:(NSError *_Nullable)error{
    
}
- (void)nativeExpressRewardedVideoAdWillVisible:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd{
    
}
- (void)nativeExpressRewardedVideoAdDidVisible:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd{
    
}
- (void)nativeExpressRewardedVideoAdWillClose:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd{
    
}
- (void)nativeExpressRewardedVideoAdDidClose:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd{
    
    [self.rewardedAd loadAdData];
    count ++;
}
- (void)nativeExpressRewardedVideoAdDidClick:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd{
    
}
- (void)nativeExpressRewardedVideoAdDidClickSkip:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd{
    
}

- (void)nativeExpressRewardedVideoAdDidPlayFinish:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd didFailWithError:(NSError *_Nullable)error{
    
}

- (void)nativeExpressRewardedVideoAdServerRewardDidSucceed:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd verify:(BOOL)verify{
    
}
- (void)nativeExpressRewardedVideoAdServerRewardDidFail:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd{
    
}


#pragma mark -- 全屏视频
-(void)loadFullVideo{
    BUNativeExpressFullscreenVideoAd *fullscreenAd = [[BUNativeExpressFullscreenVideoAd alloc] initWithSlotID:@"940033111"];
    fullscreenAd.delegate = self;
    [fullscreenAd loadAdData];
    self.fullVideoAd = fullscreenAd;
}

- (void)nativeExpressFullscreenVideoAdDidLoad:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd{
    
}
- (void)nativeExpressFullscreenVideoAd:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd didFailWithError:(NSError *_Nullable)error{
    
}

- (void)nativeExpressFullscreenVideoAdViewRenderSuccess:(BUNativeExpressFullscreenVideoAd *)rewardedVideoAd{
    
    
}


- (void)nativeExpressFullscreenVideoAdViewRenderFail:(BUNativeExpressFullscreenVideoAd *)rewardedVideoAd error:(NSError *_Nullable)error{
    
}

- (void)nativeExpressFullscreenVideoAdDidDownLoadVideo:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd{
    
}
- (void)nativeExpressFullscreenVideoAdWillVisible:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd{
    
}
- (void)nativeExpressFullscreenVideoAdDidVisible:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd{
    
}
- (void)nativeExpressFullscreenVideoAdDidClick:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd{
    
}
- (void)nativeExpressFullscreenVideoAdDidClickSkip:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd{
    
}
- (void)nativeExpressFullscreenVideoAdWillClose:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd{
    
}
- (void)nativeExpressFullscreenVideoAdDidClose:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd{
    
    [self.fullVideoAd loadAdData];
    count ++;
}

#pragma --mark  draw信息流
-(void)loadBannerAd{
    CGFloat screenWidth = CGRectGetWidth([UIScreen mainScreen].bounds);
    CGFloat bannerHeigh = screenWidth/600*150;
    BUSize *imgSize = [BUSize sizeBy:BUProposalSize_Banner600_150];
    BUNativeExpressBannerView *bannerView = [[BUNativeExpressBannerView alloc] initWithSlotID:@"940033814" rootViewController:self imgSize:imgSize adSize:CGSizeMake(screenWidth, bannerHeigh) IsSupportDeepLink:NO];
    bannerView.frame = CGRectMake(0, 100, screenWidth, bannerHeigh);
    bannerView.backgroundColor = [UIColor greenColor];
    bannerView.delegate = self;
    [self.view addSubview:bannerView];
    [bannerView loadAdData];
}

- (void)nativeExpressBannerAdViewDidLoad:(BUNativeExpressBannerView *)bannerAdView{
    
}

- (void)nativeExpressBannerAdView:(BUNativeExpressBannerView *)bannerAdView didLoadFailWithError:(NSError *_Nullable)error{
    
}
- (void)nativeExpressBannerAdViewRenderSuccess:(BUNativeExpressBannerView *)bannerAdView{
    
}

- (void)nativeExpressBannerAdViewRenderFail:(BUNativeExpressBannerView *)bannerAdView error:(NSError * __nullable)error{
    
}

- (void)nativeExpressBannerAdViewWillBecomVisible:(BUNativeExpressBannerView *)bannerAdView{
    
}
- (void)nativeExpressBannerAdViewDidClick:(BUNativeExpressBannerView *)bannerAdView{
    
}
- (void)nativeExpressBannerAdView:(BUNativeExpressBannerView *)bannerAdView dislikeWithReason:(NSArray<BUDislikeWords *> *_Nullable)filterwords{
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
